package com.example.myapplication;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MyGrid extends AppCompatActivity {

    private GridView gridView;
    private TextView emptyView;
    private Button btnGenerate;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_grid);

        // 初始化视图
        gridView = findViewById(R.id.gridView);
        emptyView = findViewById(R.id.nodata);
        btnGenerate = findViewById(R.id.btn_generate);

        // 设置空视图
        gridView.setEmptyView(emptyView);

        // 初始化适配器
        adapter = new ArrayAdapter<>(
                this,
                R.layout.grid_item,  // 自定义item布局
                dataList
        );
        gridView.setAdapter(adapter);

        // 按钮点击事件
        btnGenerate.setOnClickListener(v -> {
            // 生成三个示例数据
            dataList.clear();
            dataList.add("One");
            dataList.add("Two");
            dataList.add("Three");

            // 刷新适配器
            adapter.notifyDataSetChanged();
        });
    }
}